/* 
 * @name Property.js
 * @author : Amissah Gideon<kuulmek@yahoo.com>
 * @version 1.0
 */

var gblFields = [
    'property_group_id',
    'location_id',
    'loc_region',
    'property_group_type_id',
    'property_group_sub_type_id',
    'property_reg_no',
    'name',
    'type_of_ownership',
    'owners_name',
    'other_partners_name',
    'house_no',
    'address',
    'telephone',
    'business_no',
    'nationality',
    'location',
    'popular_area_name',
    'street_name',
    'area_code',
    'comment',
    'private_property',
    'identification',
    'type_of_business',
    'registration_no',
    'year_of_establishment',
    'dormant_business',
    'permit_no',
    'store_no',
    'no_rooms',
    'date_approval',
    'clasification',
    'no_trained_teachers',
    'no_nontrained_teachers',
    'no_class_room',
    'no_students_per_class'
]
 
var Property = {
	
	/* Server script url   */
	ssUrl : '',
    selected_row_id : null,
    property_item_detail_id : null,
    last_insert_id : null,
    grid_state : null,

   /* dataTable Object */
    objGrid: null,

	/* Function init
	 * This function is the constructor of the Object login.
	 * It initialises certain DOM ids of the login screen and listens to the user actions to perform.
	 * @param void
	 * @return void
	 * @access public
	 * */
	init:function(next_auto_increment){ //console.log(gblPrpGroupData) 
      Property.last_insert_id = next_auto_increment;
	  var dataUrl = $('#table-url').val();
      Property.objGrid = $('#propertyflex').flexigrid({
            url: dataUrl,
        	dataType: 'json',
            colModel : [
                {display: 'ID', name : 'id', width : 20, sortable : false, align: 'left', hide:true},                
                {display: 'Property Group', name : 'group_name', width : 170, sortable : false, align: 'left', hide:false},
                {display: 'Location', name : 'location_name', width : 160, sortable : false, align: 'left', hide:false},
                {display: 'Location Region', name : 'loc_region', width : 100, sortable : false, align: 'left', hide:false},
                {display: 'Area Code', name : 'location_code', width : 80, sortable : false, align: 'left', hide:false},
                {display: 'Property Registration No', name : 'property_reg_no', width : 140, sortable : false, align: 'left', hide:false},
                {display: 'Property Name', name : 'name', width : 220, sortable : false, align: 'left', hide:false},
                {display: 'Telephone', name : 'telephone', width :100, sortable : false, align: 'left', hide:false},
                {display: 'Property Type', name : 'type_name', width : 160, sortable : false, align: 'left', hide:true},
                {display: 'Property Sub Type', name : 'sub_type_name', width : 160, sortable : false, align: 'left', hide:true}
        	],
            formFields : [
        		{type:'buttom',name: 'Add', bclass: 'add',onpress : Property.handleGridEvent},
        		{separator: true},
                {type:'buttom',name: 'Edit', bclass: 'edit',onpress : Property.handleGridEvent},
                {separator: true},
        		{type:'buttom',name: 'Delete', bclass: 'delete',onpress : Property.handleGridEvent},
                {separator: true},
                {type:'buttom',name: 'View', bclass: 'view',onpress : Property.handleGridEvent},
                {separator: true},
                {type:'select',name: 'Filter', bclass: 'filter',onchange:Property.handleGridEvent,options:gblPrpGroup}
       		],
        	searchitems : [
        		//{display: 'Property Group', name : 'group_name'},
                //{display: 'Location', name : 'location_name'},
                //{display: 'Location Region', name : 'loc_region'},
                //{display: 'Area Code', name : 'location_code'},
                {display: 'Property Registration No', name : 'property_reg_no',isdefault: true},
                {display: 'Property Name', name : 'name'},
                //{display: 'Property Type', name : 'type_name'},
                //{display: 'Property Sub Type', name : 'sub_type_name'}
        	],
            checkboxSelection: true,
            columnControl:true,
        	sortname: "id",
        	sortorder: "desc",
        	usepager: true,
        	title: 'List of Properties',
        	useRp: true,
        	rp: 15,
        	showTableToggleBtn: false,
        	width: '100%',
        	height: 200
      });
      
       jLib.afterClosePopUpForm('property-item-form',function(){
         Property.selected_row_id = null;
         Property.initializePropertyForm(2); //Private Residentail House as default form of id 2
       });
       $("#property-item-form").validationEngine(); 
      
       Property.initializePropertyForm(2); //Private Residentail House as default form of id 2
       Property.initializeFormSelectChange();
       
       $('#property-save-btn').click(function(){
           Property.saveProperty();
       });
       $('#property-cancel-btn').click(function(){
           document.getElementById('property-item-form').reset();
           $.validationEngine.closePrompt('.formError',true);
           $.colorbox.close();
           Property.selected_row_id = null;
       });
       $('#property-ok-btn').click(function(){
           $.colorbox.close();
       }); 
       $('.date-picker').datepicker({
            autoSize:true,
            dateFormat: 'yy-mm-dd',
            showWeek: false,
            firstDay: 0,
            showOn: 'button',
            buttonImage: 'img/calender.png',
            buttonImageOnly: true,
            changeMonth: true,
            changeYear: true,
            minDate: new Date(1920, 1, 1)
       }); 
	},
    
    handleGridEvent: function(com, grid, value){
		if (com == 'Add') {
		   
           Property.grid_state = 'add';
           $('#property-item-form-window #property-item-form #location_id').change();// to set the property reg no
          
		   $.colorbox({
                inline:true,
                scrolling:false,
                overlayClose:false,
                escKey:false,
                top:'5%',
                title:'Property Information',
                href:"#property-item-form-window"
            });
		} 
        else if (com == 'Edit') {
          Property.grid_state = 'edit';
          if(jLib.rowSelectedCheck(grid,1)){
               Property.editProperty(jLib.getSelectedRowIds(grid));
          }
		}
        else if (com == 'Delete') {
            if(jLib.rowSelectedCheck(grid)){
                   	Property.deleteProperty(grid);
            }
		}
        else if (com == 'View') {
            if(jLib.rowSelectedCheck(grid,1)){
                   	Property.viewProperty(jLib.getSelectedRowIds(grid));
            }
		}
        else if (com == 'Filter') {
            Property.filterGrid(value);
		}
   },
   
   filterGrid:function(pvalue,grid){         
     $(Property.objGrid).flexOptions({
        params: [
            {name: 'filter', value: pvalue}
        ]
     }).flexReload();
   },
   
   initializePropertyForm: function(value){
        $('#property_group_id').val(value);
        Property.processGroupChange(value);
        Property.filterPropertyForm(value);
   },
   
   initializeFormSelectChange: function(){
        $('#property-item-form-window #property-item-form #property_group_id').change(function(){
            Property.processGroupChange(parseInt($(this).val()));
            Property.filterPropertyForm(parseInt($(this).val()));
            
            Property.generatePrpRegNo();
        });
        $('#property-item-form-window #property-item-form #property_group_type_id').change(function(){
            Property.processGroupTypeChange(parseInt($(this).val()));
        });
        
        //location Change For area code
        $('#property-item-form-window #property-item-form #location_id').change(function(){ 
            var id = $(this).val();
            // IE fix
            if(typeof id == 'string'){}
            else{
                id = '1';
            }
            var area_code = '';
            if(typeof gblLocAreaCodeData[parseInt(id)] != "undefined"){
                area_code = gblLocAreaCodeData[parseInt(id)].location_code;
            }
            $('#property-item-form-window #property-item-form #area_code').val(area_code);
            Property.generatePrpRegNo();
        });
        $('#property-item-form-window #property-item-form #location_id').change();
   },
   
   processGroupChange: function(value){
        if(value == null){
            return false;
        }
        var groupData = gblPrpGroupData[value];
        var groupTypeSet = false;
        if(groupData['group_type'] != null ){
            groupTypeSet = groupData['group_type'];
        }
        var select_1 = document.getElementById('property_group_type_id');
        select_1.options.length = 0;
        var select_2 = document.getElementById('property_group_sub_type_id');
        select_2.options.length = 0;
       // console.log(groupTypeSet);
        
        var groupSubTypeSet = false;
        for(nx in groupTypeSet){
           if(groupTypeSet[nx].group_sub_type != null){
                groupSubTypeSet = groupTypeSet[nx].group_sub_type;
           }
           break;
        }
        //console.log(groupSubTypeSet);
        //Now Populate them
        if(groupTypeSet){
            for(nx in groupTypeSet){
                var opt = document.createElement('option');
                opt.value = groupTypeSet[nx].id;
                opt.text = groupTypeSet[nx].type_name;		   
                try{ //Standard
                    select_1.add(opt,null) ;
                }
                catch(error){ //IE Only
                    select_1.add(opt) ;
                }   
            }
        }
        else{
            var opt = document.createElement('option');
            opt.value = 0;
            opt.text = '';		   
            try{ //Standard
                select_1.add(opt,null) ;
            }
            catch(error){ //IE Only
                select_1.add(opt) ;
            }   
        }
        
        if(groupSubTypeSet){
            for(nx in groupSubTypeSet){
                var opt = document.createElement('option');
                opt.value = groupSubTypeSet[nx].id;
                opt.text = groupSubTypeSet[nx].sub_type_name;		   
                try{ //Standard
                    select_2.add(opt,null) ;
                }
                catch(error){ //IE Only
                    select_2.add(opt) ;
                }   
            }
        }
        else{ 
            var opt = document.createElement('option');
            opt.value = 0;
            opt.text = '';		   
            try{ //Standard
                select_2.add(opt,null) ;
            }
            catch(error){ //IE Only
                select_2.add(opt) ;
            }  
        }
        
        Property.processGroupTypeChange();
   },
   
    processGroupTypeChange: function(value){
        var group_id = parseInt($('#property_group_id').val());
        var groupData = gblPrpGroupData[group_id];
        var select = document.getElementById('property_group_sub_type_id');
        select.options.length = 0;
        
        if(value == null){
            var opt = document.createElement('option');
            opt.value = 0;
            opt.text = '';		   
            try{ //Standard
                select.add(opt,null) ;
            }
            catch(error){ //IE Only
                select.add(opt) ;
            } 
            
            return false;
            
        }
        
        if(groupData['group_type'] != null){
            var groupTypeSet = groupData['group_type'][value];
            var groupSubTypeSet = false;
            if(groupTypeSet['group_sub_type'] != null){
                groupSubTypeSet = groupTypeSet['group_sub_type'];
            }
            //Now Populate them
            if(groupSubTypeSet){
                for(nx in groupSubTypeSet){
                    var opt = document.createElement('option');
                    opt.value = groupSubTypeSet[nx].id;
                    opt.text = groupSubTypeSet[nx].sub_type_name;		   
                    try{ //Standard
                        select.add(opt,null) ;
                    }
                    catch(error){ //IE Only
                        select.add(opt) ;
                    }   
                }
            }
            else{
                var opt = document.createElement('option');
                opt.value = 0;
                opt.text = '';		   
                try{ //Standard
                    select.add(opt,null) ;
                }
                catch(error){ //IE Only
                    select.add(opt) ;
                } 
            }
        }
        else{
            var opt = document.createElement('option');
                opt.value = 0;
                opt.text = '';		   
                try{ //Standard
                    select.add(opt,null) ;
                }
                catch(error){ //IE Only
                    select.add(opt) ;
                } 
        }
        
   },
   
   filterPropertyForm: function(value){
       $('#property-item-form-window #property-item-form div.content div.row').each(function(){
            $(this).css('display','block');
       });  
       var groupData = gblPrpGroupData[value];
       var fields = groupData.fields;
       if(fields.length > 0){
            var diff_arr = jLib.array_diff(fields, gblFields)
            for(var x = 0; x < diff_arr.length; x++){
                var diff = diff_arr[x];
                //console.log(diff);
                $('#property-item-form-window #property-item-form div.content div.'+diff+'').css('display','none'); 
            }
       }
       $.colorbox.resize();
       
   },
   
   
    editProperty:function(row_ids){
         var load_id = row_ids[0];
         var url = $('#load-url').val();
         var query = 'ids=' + load_id;
         $.post(url, query, function(response){  
			if(response.code === 0){
			   var dataPropertyItem = response.data.PropertyItem;
               var dataPropertyLocation = response.data.Location;
               var dataPropertyItemDetail = response.data.PropertyItemDetail;
               var dataPropertyGroup = response.data.PropertyGroup;
               var dataPropertyGroupType = response.data.PropertyGroupType;
               var dataPropertyGroupSubType = response.data.PropertyGroupSubType;
               
               Property.property_item_detail_id = dataPropertyItemDetail.id
               //$('#property-item-form #item_id').val(dataPropertyItem.id);
               //$('#property-item-form #item_detail_id').val(dataPropertyItemDetail.id);
            
               $('#property-item-form #property_group_id').val(dataPropertyGroup.id);
               Property.processGroupChange(dataPropertyGroup.id);
               Property.processGroupTypeChange(dataPropertyGroupType.id);
               Property.filterPropertyForm(dataPropertyGroup.id);
               $('#property-item-form #location_id').val(dataPropertyLocation.id); 
               $('#property-item-form #loc_region').val(dataPropertyItem.loc_region);
               $('#property-item-form #property_group_type_id').val(dataPropertyGroupType.id);
               $('#property-item-form #property_group_sub_type_id').val(dataPropertyGroupSubType.id);
               $('#property-item-form #property_reg_no').val(dataPropertyItem.property_reg_no);
               $('#property-item-form #latitude').val(dataPropertyItem.latitude);
               $('#property-item-form #longitude').val(dataPropertyItem.longitude);
               $('#property-item-form #elevation').val(dataPropertyItem.elevation);
               $('#property-item-form #name').val(dataPropertyItem.name);
               $('#property-item-form #type_of_ownership').val(dataPropertyItem.type_of_ownership);
               $('#property-item-form #owners_name').val(dataPropertyItem.owners_name);
               $('#property-item-form #other_partners_name').val(dataPropertyItem.other_partners_name);
               $('#property-item-form #house_no').val(dataPropertyItem.house_no);
               $('#property-item-form #address').val(dataPropertyItem.address);
               $('#property-item-form #telephone').val(dataPropertyItem.telephone);
               $('#property-item-form #business_no').val(dataPropertyItem.business_no);
               $('#property-item-form #nationality').val(dataPropertyItem.nationality);
               $('#property-item-form #location').val(dataPropertyItem.location);
               $('#property-item-form #popular_area_name').val(dataPropertyItem.popular_area_name);
               $('#property-item-form #street_name').val(dataPropertyItem.street_name);
               $('#property-item-form #area_code').val(dataPropertyItem.area_code);
               $('#property-item-form #comment').val(dataPropertyItem.comment);
               $('#property-item-form #private_property').val(dataPropertyItemDetail.private_property);
               $('#property-item-form #identification').val(dataPropertyItemDetail.identification);
               $('#property-item-form #type_of_business').val(dataPropertyItemDetail.type_of_business);
               $('#property-item-form #registration_no').val(dataPropertyItemDetail.registration_no);
               $('#property-item-form #permit_no').val(dataPropertyItemDetail.permit_no);
               $('#property-item-form #year_of_establishment').val(dataPropertyItemDetail.year_of_establishment);
               $('#property-item-form #store_no').val(dataPropertyItemDetail.store_no);
               $('#property-item-form #dormant_business').val(dataPropertyItemDetail.dormant_business);
               $('#property-item-form #permit_no').val(dataPropertyItemDetail.permit_no);  
               $('#property-item-form #store_no').val(dataPropertyItemDetail.store_no);   
               $('#property-item-form #no_rooms').val(dataPropertyItemDetail.no_rooms);   
               $('#property-item-form #date_approval').val(dataPropertyItemDetail.date_approval);  
               $('#property-item-form #clasification').val(dataPropertyItemDetail.clasification); 
               $('#property-item-form #no_trained_teachers').val(dataPropertyItemDetail.no_trained_teachers);   
               $('#property-item-form #no_class_room').val(dataPropertyItemDetail.no_class_room);   
               $('#property-item-form #no_nontrained_teachers').val(dataPropertyItemDetail.no_nontrained_teachers);    
               $('#property-item-form #no_students_per_class').val(dataPropertyItemDetail.no_students_per_class);             
               
               
               Property.selected_row_id = load_id;
               
               $.colorbox({
                  inline:true,
                  scrolling:false,
                  overlayClose:false,
                  escKey:false,
                  top:'5%',
                  title:'Property Information',
                  href:"#property-item-form-window"
               });
			}
			/* When there are Errors */
			else if(response.code === 1){
			   /* Alert the user that the server response*/
			   jLib.setFeedback(response.mesg, 'error_message', false);
			}  
         },'json');
       },
       
       saveProperty:function(){
             /* Validate the form*/
              var validationStatus =  $('#property-item-form').validationEngine({returnIsValid:true});
               /* When the Validation Status is true meaning that the data input are correct then the data can be processed. */
              if(validationStatus){
                     
                     var query = 'model=Property&row_id='+Property.selected_row_id+'&property_detail_id='+ Property.property_item_detail_id +'&'+$('#property-item-form').serialize();
                     /* Send the data to the server and handle the server response */                     
                     $.post( $('#add-edit-url').val(), query, function(response){
						var txt = '';
                        if(typeof response.mesg  == 'object'){
        					for(megTxt in response.mesg){
        						txt += response.mesg[megTxt]+'<br />';
        					}
        				}
        				else{
        					txt = response.mesg
        				}
                        
						if(response.code === 0){					   
						   jLib.setFeedback(txt, 'success_message', false);
                           /* Update Next auto increment id*/
                           Property.last_insert_id = response.next_auto_increment;
                                                      
                           if(Property.grid_state == 'edit'){
                                var groupName = '';
                                var groupTypeSetName = '';
                                var groupSubTypeSetName = '';
                                
                                var group_id = parseInt($('#property-item-form  #property_group_id').val());
                                var group_type_id = parseInt($('#property-item-form  #property_group_type_id').val());
                                var group_sub_type_id = parseInt($('#property-item-form  #property_group_sub_type_id').val());
                                
                                var groupData = gblPrpGroupData[group_id];
                                groupName = groupData.group_name;
                                if(groupData['group_type'] != null){
                                   var groupTypeSet = groupData['group_type'][group_type_id];
                                   groupTypeSetName = groupTypeSet.type_name;
                                    if(groupTypeSet['group_sub_type'] != null){
                                        groupSubTypeSet = groupTypeSet['group_sub_type'][group_sub_type_id];
                                        groupSubTypeSetName = groupSubTypeSet.sub_type_name;
                                    }
                                }
                                
                                update_data = {
                                   'group_name': groupName,
                                   'location_name': gblLocData[$('#property-item-form  #location_id').val()],
                                   'loc_region':$('#property-item-form  #loc_region').val(),
                                   'location_code':$('#property-item-form  #area_code').val(),
                                   'property_reg_no': $('#property-item-form  #property_reg_no').val(),
                                   'name': $('#property-item-form  #name').val(),
                                   'type_name': groupTypeSetName,
                                   'sub_type_name':groupSubTypeSetName
                                };
                                jLib.updateGridData(Property.objGrid, update_data);
                                jLib.highlightRow(Property.objGrid);
                           }
                           else{
                                $(Property.objGrid).flexReload();    
                           }
                           document.getElementById('property-item-form').reset();
                           $.validationEngine.closePrompt('.formError',true)
						   $.colorbox.close();
                           Property.selected_row_id = null;
                           Property.property_item_detail_id = null;
                           //Property.initializePropertyForm(2); //Private Residentail House as default form of id 2
						}
						else if(response.code === 1){
						   jLib.setFeedback(txt, 'error_message', false);
						}
                    },'json'); 
              } 
       },
       
       deleteProperty:function(grid){
            var url = $('#delete-url').val();
            jLib.do_delete(url,grid);
       },
       
       viewProperty:function(row_ids){
         var load_id = row_ids[0];
         var url = $('#load-url').val();
         var query = 'ids=' + load_id;
         $.post(url, query, function(response){  
			if(response.code === 0){
			   var dataPropertyItem = response.data.PropertyItem;
               var dataPropertyLocation = response.data.Location;
               var dataPropertyItemDetail = response.data.PropertyItemDetail;
               var dataPropertyGroup = response.data.PropertyGroup;
               var dataPropertyGroupType = response.data.PropertyGroupType;
               var dataPropertyGroupSubType = response.data.PropertyGroupSubType;
               
               $('#property-view-window div.content div.row').each(function(){
                    $(this).css('display','block');
               });
               
               var groupData = gblPrpGroupData[dataPropertyGroup.id];
               var fields = groupData.fields;
               if(fields.length > 0){
                    var diff_arr = jLib.array_diff(fields, gblFields)
                    for(var x = 0; x < diff_arr.length; x++){
                        var diff = diff_arr[x];
                        //console.log(diff);
                        $('#property-view-window div.content div.'+diff+'').css('display','none'); 
                    }
               }
               var property_group_type_id = dataPropertyGroupType.type_name ? dataPropertyGroupType.type_name :  '&nbsp;';
               var property_group_sub_type_id = dataPropertyGroupSubType.sub_type_name ? dataPropertyGroupSubType.sub_type_name : '&nbsp;';
               var location_name = dataPropertyLocation.location_name ? dataPropertyLocation.location_name : '&nbsp;';
               $('#property-view-window div.content div.property_group_id .data').html('&nbsp;'+dataPropertyGroup.group_name);
               $('#property-view-window div.content div.location_id .data').html('&nbsp;'+location_name);
               $('#property-view-window div.content div.loc_region .data').html('&nbsp;'+dataPropertyItem.loc_region);
               $('#property-view-window div.content div.property_group_type_id .data').html('&nbsp;'+property_group_type_id);
               $('#property-view-window div.content div.property_group_sub_type_id .data').html('&nbsp;'+property_group_sub_type_id);
               $('#property-view-window div.content div.property_reg_no .data').html('&nbsp;'+dataPropertyItem.property_reg_no);
               $('#property-view-window div.content div.property_latitude .data').html('&nbsp;'+dataPropertyItem.latitude);
               $('#property-view-window div.content div.property_longitude .data').html('&nbsp;'+dataPropertyItem.longitude);
               $('#property-view-window div.content div.property_elevation .data').html('&nbsp;'+dataPropertyItem.elevation);
               $('#property-view-window div.content div.name .data').html('&nbsp;'+dataPropertyItem.name);
               $('#property-view-window div.content div.type_of_ownership .data').html('&nbsp;'+dataPropertyItem.type_of_ownership);
               $('#property-view-window div.content div.owners_name .data').html('&nbsp;'+dataPropertyItem.owners_name);
               $('#property-view-window div.content div.other_partners_name .data').html('&nbsp;'+dataPropertyItem.other_partners_name);
               $('#property-view-window div.content div.house_no .data').html('&nbsp;'+dataPropertyItem.house_no);
               $('#property-view-window div.content div.address .data').html('&nbsp;'+dataPropertyItem.address);
               $('#property-view-window div.content div.telephone .data').html('&nbsp;'+dataPropertyItem.telephone);
               $('#property-view-window div.content div.business_no .data').html('&nbsp;'+dataPropertyItem.business_no);
               $('#property-view-window div.content div.nationality .data').html('&nbsp;'+dataPropertyItem.nationality);
               $('#property-view-window div.content div.location .data').html('&nbsp;'+dataPropertyItem.location);
               $('#property-view-window div.content div.popular_area_name .data').html('&nbsp;'+dataPropertyItem.popular_area_name);
               $('#property-view-window div.content div.street_name .data').html('&nbsp;'+dataPropertyItem.street_name);
               $('#property-view-window div.content div.area_code .data').html('&nbsp;'+dataPropertyItem.area_code);
               $('#property-view-window div.content div.comment .data').html('&nbsp;'+dataPropertyItem.comment);
               $('#property-view-window div.content div.private_property .data').html('&nbsp;'+dataPropertyItemDetail.private_property);
               $('#property-view-window div.content div.identification .data').html('&nbsp;'+dataPropertyItemDetail.identification);
               $('#property-view-window div.content div.type_of_business .data').html('&nbsp;'+dataPropertyItemDetail.type_of_business);
               $('#property-view-window div.content div.registration_no .data').html('&nbsp;'+dataPropertyItemDetail.registration_no);
               $('#property-view-window div.content div.permit_no .data').html('&nbsp;'+dataPropertyItemDetail.permit_no);
               $('#property-view-window div.content div.year_of_establishment .data').html('&nbsp;'+dataPropertyItemDetail.year_of_establishment);
               $('#property-view-window div.content div.store_no .data').html('&nbsp;'+dataPropertyItemDetail.store_no);
               $('#property-view-window div.content div.dormant_business .data').html('&nbsp;'+dataPropertyItemDetail.dormant_business); 
               $('#property-view-window div.content div.permit_no .data').html('&nbsp;'+dataPropertyItemDetail.permit_no);  
               $('#property-view-window div.content div.store_no .data').html('&nbsp;'+dataPropertyItemDetail.store_no);   
               $('#property-view-window div.content div.no_rooms .data').html('&nbsp;'+dataPropertyItemDetail.no_rooms);   
               $('#property-view-window div.content div.date_approval .data').html('&nbsp;'+dataPropertyItemDetail.date_approval);  
               $('#property-view-window div.content div.clasification .data').html('&nbsp;'+dataPropertyItemDetail.clasification); 
               $('#property-view-window div.content div.no_trained_teachers .data').html('&nbsp;'+dataPropertyItemDetail.no_trained_teachers);   
               $('#property-view-window div.content div.no_class_room .data').html('&nbsp;'+dataPropertyItemDetail.no_class_room);   
               $('#property-view-window div.content div.no_nontrained_teachers .data').html('&nbsp;'+dataPropertyItemDetail.no_nontrained_teachers);    
               $('#property-view-window div.content div.no_students_per_class .data').html('&nbsp;'+dataPropertyItemDetail.no_students_per_class);               
               
               $.colorbox({
                  inline:true,
                  scrolling:false,
                  overlayClose:false,
                  escKey:false,
                  top:'5%',
                  title:'Property Information',
                  href:"#property-view-window"
               });
               
               $.colorbox.resize();
			}
			/* When there are Errors */
			else if(response.code === 1){
			   /* Alert the user that the server response*/
			   jLib.setFeedback(response.mesg, 'error_message', false);
			}  
         },'json');
       },
       
       
       generatePrpRegNo: function(){
            var group_id = $('#property-item-form-window #property-item-form #property_group_id').val();
            var loc_id = $('#property-item-form-window #property-item-form #location_id').val();
            // IE fix
            if(typeof loc_id == 'string'){}
            else{
                loc_id = '1';
            }
            //console.log(typeof loc_id);
            
            var group_code = gblPrpGroupData[parseInt(group_id)].group_code;
           var loc_code = '';
           if(typeof gblLocAreaCodeData[parseInt(id)] != "undefined"){
               var loc_code = gblLocAreaCodeData[parseInt(loc_id)].location_code;
           }

            if(Property.grid_state == 'edit'){
                var temp = $('#property-item-form-window #property-item-form #property_reg_no').val()
                var temp_arr = temp.split(' / ');
                var id = temp_arr[3];
            }
            else{
                 var id = Property.last_insert_id;
            }

           var new_reg_no = '';
            if(loc_code.length > 0){
                new_reg_no = glb_reg_no_prefix+'/'+group_code+'/'+loc_code+'/'+id;
            }
           else{
                new_reg_no = glb_reg_no_prefix+'/'+group_code+'/'+id;
            }

            $('#property-item-form-window #property-item-form #property_reg_no').val(new_reg_no);
       }
   
};

/* when the page is loaded */
$(document).ready(function() {
	Property.init(glb_next_auto_increment);
});